
/**
Copyright 2008, Mark Arvidson

This file is part of EwaScorecard.

EwaScorecard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EwaScorecard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
 */
import javax.microedition.lcdui.*;

public class AddGymnastForm extends Form implements CommandListener {

    // Gymnast info form
    private TextField gymnastNumber = new TextField("Gymnast #:", null, 5, TextField.NUMERIC);
    private TextField gymnastName = new TextField("Gymnast Name:", null, 25, TextField.ANY);
    private TextField gymnastGym = new TextField("Team:", "EWA", 25, TextField.ANY);
    private Command backCommand = new Command("Done", Command.BACK, 0);
    private Command okCommand = new Command("Next", Command.OK, 0);
    private Command deleteCommand = new Command("Delete", Command.ITEM, 0);
    private MeetData meetData;
    private EwaScorecard host;
    private float[] scores;

    public AddGymnastForm(EwaScorecard host, MeetData meetData) {
        super("Gymnast Info");
        this.meetData = meetData;
        this.host = host;
//        append(gymnastNumber);
        append(gymnastName);
        append(gymnastGym);
        addCommand(backCommand);
        addCommand(okCommand);
        setCommandListener(this);
    }

    public AddGymnastForm(EwaScorecard host, MeetData meetData,
            TeamData team, GymnastData gymnast) {
        this(host, meetData);
//      gymnastNumber.setString(gymnast.getNumber());
        gymnastName.setString(gymnast.getName());
        gymnastGym.setString(team.getName());
        scores = gymnast.getScores();
        team.deleteGymnast(gymnast);
        if (team.gymnasts().size() == 0) {
            meetData.teams().removeElement(team);
        }
        addCommand(deleteCommand);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == okCommand) {
            addGymnast();
        } else if (c == backCommand) {
            if (gymnastName.getString().length() > 0) {
                addGymnast();
            }
            host.setMeetData(meetData);
        } else if (c == deleteCommand) {
            scores = null;
            host.setMeetData(meetData);
        }

    }

    private void addGymnast() {
        int number = 0;
        try {
            number = Integer.parseInt(gymnastNumber.getString());
        } catch (NumberFormatException e) {
        }

        meetData.addGymnast(gymnastGym.getString(), number, 
                gymnastName.getString(), scores);
        
        scores = null;
        gymnastNumber.setString(null);
        gymnastName.setString(null);
        Display.getDisplay(host).setCurrentItem(gymnastName);
    }
}
